﻿using MusicPlayerPlus.Interface.Base;
using MusicPlayerPlus.Interface.Model.Enum;
using MusicPlayerPlus.Interface.Model.Muisc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace MusicPlayerPlus.PlugIn.Control
{
    public class PlugIn : InterfaceBase,
        //Input
        MusicPlayerPlus.Interface.Base.Action.Input.IPlayOrderChangeAction,
        MusicPlayerPlus.Interface.Base.Action.Input.IPlayStateChangeAction,
        //MusicPlayerPlus.Interface.Base.Action.Input.IPlayMusicChange,
        MusicPlayerPlus.Interface.Base.Action.Input.IMuiscListChange,
        MusicPlayerPlus.Interface.Base.Action.Input.IPreviousTrackAction,
        MusicPlayerPlus.Interface.Base.Action.Input.INextTrackAction,

        //Output
        MusicPlayerPlus.Interface.Base.Action.Output.ISetPlayMusicAction,
        MusicPlayerPlus.Interface.Base.Action.Output.IGetPlayMusicAction,
        MusicPlayerPlus.Interface.Base.Action.Output.IGetMusicListAction
    {
        #region Input

        /// <summary>
        /// 播放顺序改变
        /// </summary>
        public void PlayPlayOrderChange(MediaPlayPlayOrder val)
        {
            playOrder = val;
        }
        /// <summary>
        /// 播放状态改变
        /// </summary>
        public void PlayStateChange(MediaPlayState val)
        {
            switch (val)
            {
                case MediaPlayState.Play:
                    break;
                case MediaPlayState.Pause:
                    break;
                case MediaPlayState.Stop:
                    break;
                case MediaPlayState.End:
                    //下一首
                    PlayNewMusic(true, true);
                    break;
                case MediaPlayState.Error:
                    //下一首
                    PlayNewMusic(true, true);
                    break;
                case MediaPlayState.BufferingStart:
                    break;
                case MediaPlayState.BufferingEnd:
                    break;
                default:
                    break;
            }
        }
        ///// <summary>
        ///// 播放的歌曲改变时
        ///// </summary>
        //public void PlayMusicChange(MusicItem item)
        //{
        //}
        /// <summary>
        /// 播放中的列表改变时
        /// </summary>
        public void MuiscListChange(Dictionary<Guid, MusicItem> list)
        {
            //重新 初始化播放列表 及歌曲
            PlayNewMusic(true, false);
        }
        /// <summary>
        /// 上一首
        /// </summary>
        public void PreviousTrack()
        {
            PlayNewMusic(false, false);
        }
        /// <summary>
        /// 下一首
        /// </summary>
        public void NextTrack()
        {
            PlayNewMusic(true, false);
        }

        #endregion

        #region Output

        /// <summary>
        /// 设置播放的音乐
        /// </summary>
        public Action<MusicItem> SetPlayMusic_Action { private get; set; }
        /// <summary>
        /// 获取列表
        /// </summary>
        public Func<IList<MusicItem>> GetMusicList_Action { private get; set; }
        /// <summary>
        /// 获取播放的音乐
        /// </summary>
        public Func<MusicItem> GetPlayMusic_Action { private get; set; }
        #endregion

        #region Object
        /// <summary>
        /// 播放顺序
        /// </summary>
        MediaPlayPlayOrder playOrder = MediaPlayPlayOrder.Continuous;

        #endregion


        #region Method
        /// <summary>
        /// 播放新的一首歌
        /// </summary>
        private void PlayNewMusic(bool isNext, bool isauto)
        {
            IList<MusicItem> playList = GetMusicList_Action();
            MusicItem playmusic = GetPlayMusic_Action();

            MusicItem newmusic = playmusic;

            try
            {
                //判断怎么播放下一首了
                switch (playOrder)
                {
                    case MediaPlayPlayOrder.RepeatOne:
                        if (!isauto)
                        {
                            newmusic = GetNewMusic(playmusic, playList, isNext);
                        }
                        break;
                    case MediaPlayPlayOrder.Continuous:
                        newmusic = GetNewMusic(playmusic, playList, isNext);
                        break;
                    case MediaPlayPlayOrder.Shuffle:
                        Random r = new Random();
                        int r_index = r.Next(playList.Count);
                        newmusic = playList[r_index];
                        break;
                    default:
                        break;
                }
            }
            catch (Exception)
            {

            }

            SetPlayMusic_Action(newmusic);
        }


        /// <summary>
        /// 获取新的歌曲
        /// </summary>
        private MusicItem GetNewMusic(MusicItem playmusic, IList<MusicItem> playList, bool isNext)
        {
            int item_index1 = playList.IndexOf(playmusic);
            int new_index1 = GetNewIndex(isNext, item_index1, playList.Count);
            return playList[new_index1];
        }
        /// <summary>
        /// 获取新的歌曲
        /// </summary>
        /// <param name="isNext"></param>
        /// <param name="thisindex"></param>
        /// <param name="count"></param>
        /// <returns></returns>
        private int GetNewIndex(bool isNext, int thisindex, int count)
        {
            if (thisindex >= 0)
            {
                if (isNext)
                {
                    int new_index = thisindex + 1;
                    if (new_index >= count)
                    {
                        new_index = 0;
                    }
                    return new_index;
                }
                else
                {
                    int new_index = thisindex - 1;
                    if (new_index < 0)
                    {
                        new_index = count - 1;
                    }
                    return new_index;
                }
            }
            else
            {
                return 0;
            }
        }

        #endregion
    }
}
